Skip to content

describe equality constraints#2254

Open
tlyu wants to merge 1 commit intorust-lang:masterfrom
tlyu:equality-bounds
Open

describe equality constraints#2254
tlyu wants to merge 1 commit intorust-lang:masterfrom
tlyu:equality-bounds

Conversation

@tlyu
Copy link
Copy Markdown
Contributor

@tlyu tlyu commented May 3, 2026

Equality constraints on associated types have been supported for a long time, but lacking in documentation.

Much of this is based on RFC 195.

fixes #931

Equality constraints on associated types have been supported for a long
time, but lacking in documentation.

Much of this is based on RFC 195.

fixes rust-lang#931
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label May 3, 2026
@DanielEScherzer
Copy link
Copy Markdown
Contributor

@rustbot label +A-generics "New content"

@rustbot rustbot added A-generics Area: generics New Content Missing features or aspects of language not currently documented. labels May 4, 2026
Comment thread src/trait-bounds.md
```

r[bound.equality.trait-object]
Equality constraints are *required* for generic [trait objects].
Copy link
Copy Markdown
Member

@fmease fmease May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's too imprecise, they aren't required per se.

They are required for associated types specifically or rephrased, every associated type from the trait itself or from any of its supertraits needs to be constrained in the trait object type ... unless it has a where Self: Sized; clause or was already constrained via supertrait bounds.

In any case, isn't this partially or fully duplicating facts from the Dyn compatibility section and thus redundant?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equality constraints appear to currently only be valid for associated types, as stated earlier in the proposed text. Stating that they are required for trait objects implies that they apply only to associated types.

In any case, isn't this partially or fully duplicating facts from the Dyn compatibility section and thus redundant?

Where is the requirement to fully specify associated types on trait objects in the existing Reference? It doesn't logically belong in the section on dyn compatibility, because a generic trait can be dyn compatible.

There is a case for describing this requirement in the section on trait objects, but I think it makes more sense to put it with the other trait bounds.

Comment thread src/trait-bounds.md
Comment on lines +114 to +121
Equality constraints cannot currently be expressed using a [WhereClause].

```rust,compile_fail
# trait Tr { type Output; }
# struct S {}
# impl Tr for S { type Output = S; }
// error: equality constraints are not yet supported in `where` clauses
fn uses_where<T>() where T::Output = S {}
Copy link
Copy Markdown
Member

@fmease fmease May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether it makes sense to document something that isn't supported, especially since the Reference grammar already implies that.

Technically speaking, equality predicates like this are still part of the language in a gray area since they're syntactically valid in rustc. However, that'll change soon since T-lang has accepted my proposal to remove them. See rust-lang/rust#153513, I just need to tie up some loose ends.

That's why I initially clicked on this PR, I thought you were adding them to the Reference grammar.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of support for equality constraints in where clauses is specifically called out in RFC 195 (even though it is internally inconsistent on about it). I think it's useful to mention it anyway, because where clauses otherwise generally have superset of the capabilities of bounds attached inline to generic parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-generics Area: generics New Content Missing features or aspects of language not currently documented. S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document associated type equality constraint

4 participants